package com.cscj.android.repository.settings.impl

import android.graphics.Bitmap
import androidx.room.withTransaction
import com.cscj.android.repository.settings.Settings
import com.cscj.android.repository.db.CacheDatabase
import com.cscj.android.repository.db.dao.SearchEngineDao
import com.cscj.android.repository.db.entity.SearchEngineEntity
import com.cscj.android.utils.BitmapUtils.asBase64String
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.firstOrNull
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

class SearchSettingImpl : Settings.SearchSetting, KoinComponent {

    companion object {
        //define id.
    }

    private val initialData by lazy {
        listOf(
            SearchEngineEntity(
                id = 1, name = "百度", urlPattern = "https://www.baidu.com/s?wd=", preferred = true
            ),
            SearchEngineEntity(
                id = 2,
                name = "360搜索",
                urlPattern = "https://m.so.com/s?q=",
                preferred = false
            ),
            SearchEngineEntity(
                id = 3,
                name = "无追",
                urlPattern = "https://www.wuzhuiso.com/s?q=",
                preferred = false
            ),
            SearchEngineEntity(
                id = 4,
                name = "Bing",
                urlPattern = "https://cn.bing.com/search?q=",
                preferred = false
            ),
            SearchEngineEntity(
                id = 5,
                name = "夸克",
                urlPattern = "https://quark.sm.cn/s?q=",
                preferred = false
            ),
            SearchEngineEntity(
                id = 6,
                name = "Google",
                urlPattern = "https://www.google.com.hk/search?q=",
                preferred = false
            ),
        )
    }

    private val searchEngineDao by inject<SearchEngineDao>()

    private val database by inject<CacheDatabase>()

    override suspend fun init() {
        database.withTransaction {
            val currentEngine = currentSearchEngine().firstOrNull()
            searchEngineDao.batchInsert(initialData)
            if(currentEngine != null){
                switchSearchEngine(currentEngine)
            }
        }

    }

    override fun currentSearchEngine(): Flow<SearchEngineEntity?> {
        return searchEngineDao.currentEngine()
    }

    override suspend fun switchSearchEngine(engineEntity: SearchEngineEntity) {
        database.withTransaction {
            searchEngineDao.updatePreferred(false)
            searchEngineDao.updatePreferredById(engineEntity.id, true)
        }
    }

    override suspend fun newEngine(
        name: String, urlPattern: String, preferred: Boolean, icon: Bitmap?
    ) {
        searchEngineDao.upsertSearchEngine(
            SearchEngineEntity(
                name = name,
                urlPattern = urlPattern,
                preferred = preferred,
                icon = icon?.asBase64String()
            )
        )
    }
}